package com.olap.starter.controller.admin.luckycola.services;

import com.olap.starter.common.util.SqlUtils;
import com.olap.starter.controller.admin.luckycola.DBDto;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AsyncDBServices {

    public void getSql(DBDto dbDto, Map<String, Map<String, Object>> dataSourceMap, List<String> exSqlList) {
        for (Map<String, Object> stringObjectMap : dbDto.getList()) {
            String COLUMN_NAME = stringObjectMap.get("COLUMN_NAME").toString().toLowerCase();
            Object COLUMN_NAME_OLD = stringObjectMap.get("COLUMN_NAME_OLD");
            Object delete = stringObjectMap.get("delete");
            Object columnDefault = stringObjectMap.get("COLUMN_DEFAULT");
            Object DEFAULT = stringObjectMap.get("DEFAULT");
            if (COLUMN_NAME_OLD == null && delete != null) {
                continue;
            }
            if (columnDefault == null) {
                columnDefault = "";
            } else {
                columnDefault = " DEFAULT " + columnDefault.toString().trim() + " ";
            }
            if ("1".equals(DEFAULT)) {
                addDefault(dbDto, COLUMN_NAME, dataSourceMap, exSqlList);
            } else if (COLUMN_NAME_OLD == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN " + COLUMN_NAME + " " + stringObjectMap.get("COLUMN_TYPE") + columnDefault + "  COMMENT '" + stringObjectMap.get("COLUMN_COMMENT") + "'");
            } else if (delete != null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " DROP COLUMN " + COLUMN_NAME);
            } else if (COLUMN_NAME.equals(COLUMN_NAME_OLD)) {
                //字段相同，更新类型或者备注
                StringBuilder sb = new StringBuilder();
                Map<String, Object> stringObjectMap1 = dataSourceMap.get(COLUMN_NAME);
                if (!stringObjectMap.get("DATA_TYPE").equals(stringObjectMap1.get("DATA_TYPE")) ||
                        (stringObjectMap.get("CHARACTER_MAXIMUM_LENGTH") != null && !stringObjectMap.get("CHARACTER_MAXIMUM_LENGTH").toString().equals(stringObjectMap1.get("CHARACTER_MAXIMUM_LENGTH").toString()))) {
                    sb.append(" ").append(stringObjectMap.get("COLUMN_TYPE").toString());
                    sb.append(columnDefault);
                }
                if (!stringObjectMap.get("COLUMN_COMMENT").equals(stringObjectMap1.get("COLUMN_COMMENT"))) {
                    if (sb.length() == 0) {
                        sb.append(" ").append(stringObjectMap.get("COLUMN_TYPE").toString());
                    }
                    sb.append("  COMMENT '").append(stringObjectMap.get("COLUMN_COMMENT")).append("'");
                    sb.append(columnDefault);
                }
                if (sb.length() > 0) {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " MODIFY COLUMN " + COLUMN_NAME +   sb);
                }
            } else if (!COLUMN_NAME.equals(COLUMN_NAME_OLD.toString())) {
                //字段不相同，直接更新
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " CHANGE COLUMN " + COLUMN_NAME_OLD + " " + COLUMN_NAME + " " + stringObjectMap.get("COLUMN_TYPE") + columnDefault + "  COMMENT '" + stringObjectMap.get("COLUMN_COMMENT") + "'");
            }
        }
        exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " COMMENT = '" + dbDto.getDesc() + "'");
        if ("1".equals(dbDto.getIsAdd())) {
            exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " DROP COLUMN 欧莱普特殊建表字段");
        }
    }

    private void addDefault(DBDto dbDto, String COLUMN_NAME, Map<String, Map<String, Object>> dataSourceMap, List<String> exSqlList) {
        if (COLUMN_NAME.equals("creator")) {
            if (dataSourceMap.get("creator") == null) {
                if (dataSourceMap.get("create_user") == null) {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN creator VARCHAR(255) COMMENT '创建者'");
                } else {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " CHANGE COLUMN create_user creator VARCHAR(255) COMMENT '创建者'");
                }
            }
        } else if (COLUMN_NAME.equals("updater")) {
            if (dataSourceMap.get("updater") == null) {
                if (dataSourceMap.get("update_user") == null) {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN updater VARCHAR(255) COMMENT '更新者'");
                } else {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " CHANGE COLUMN update_user updater VARCHAR(255) COMMENT '更新者'");
                }
            }
        } else if (COLUMN_NAME.equals("create_time")) {
            if (dataSourceMap.get("create_time") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'");
            } else {
                Map<String, Object> stringObjectMap1 = dataSourceMap.get("create_time");
                if (stringObjectMap1.get("COLUMN_DEFAULT") == null) {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " CHANGE COLUMN create_time create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'");
                }
            }
        } else if (COLUMN_NAME.equals("update_time")) {
            if (dataSourceMap.get("update_time") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'");
            } else {
                Map<String, Object> stringObjectMap1 = dataSourceMap.get("update_time");
                if (stringObjectMap1.get("COLUMN_DEFAULT") == null) {
                    exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " CHANGE COLUMN update_time update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'");
                }
            }
        } else if (COLUMN_NAME.equals("id")) {
            if (dataSourceMap.get("id") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + " ADD COLUMN id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键'");
            }
        } else if ("deleted".equals(COLUMN_NAME)) {
            if (dataSourceMap.get("deleted") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + "  ADD COLUMN deleted bit DEFAULT 0 COMMENT '是否删除'");
            }
        } else if ("tenant_id".equals(COLUMN_NAME)) {
            if (dataSourceMap.get("tenant_id") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + "  ADD COLUMN tenant_id bigint DEFAULT 0 COMMENT '租户编号'");
            }
        } else if ("remark".equals(COLUMN_NAME)) {
            if (dataSourceMap.get("remark") == null) {
                exSqlList.add(SqlUtils.ALTER_TABLE + dbDto.getTableName() + "  ADD COLUMN remark VARCHAR(255) COMMENT '租户编号'");
            }
        }
    }
}
